#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/spi.h"

#include "wizchip_conf.h"
#include "w5500.h"
#include "w5500_spi.h"
#include "socket.h"
#include "loopback.h"

#define SOCKET_ID 0
#define ETHERNET_BUF_MAX_SIZE (1024 * 2)
#define onenet_id "1050928010"  //Device ID
#define apikey    "j=4xBH0JKRFqcDOXWdHe0uUTJ8g=" //APK

void network_init(void);
uint32_t HTTP_PostPkt(char *pkt, char *key, char *devid, char *dsid, char *val);
void http_client(void);

wiz_NetInfo net_info = {
    .mac = {0x00, 0x08, 0xdc, 0x16, 0xed, 0x2e},
    .ip = {192, 168, 1, 10},
    .sn = {255, 255, 255, 0},
    .gw = {192, 168, 1, 1},
    .dns = {8, 8, 8, 8},
    .dhcp = NETINFO_STATIC};
wiz_NetInfo get_info;
static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {0,};
static uint8_t onenet_server_ip[4]={183., 230, 40, 33};
static uint8_t onenet_port = 80;

int main()                                                          
{   
    stdio_init_all();
    sleep_ms(2000);
    network_init();

    while(true)
    {
        http_client();
        sleep_ms(500);
    }
    
}

void network_init(void)
{
    uint8_t temp;
    wizchip_initialize();
    printf("W5500 http test example.\r\n");
    sleep_ms(2000);
    wizchip_setnetinfo(&net_info);
    print_network_information(get_info);
    sleep_ms(2000);   
}

void http_client()
{
    uint16_t anyport = 50000;
    uint16_t len;
    uint8_t test=25;
    
    switch (getSn_SR(SOCKET_ID))
    {
    case SOCK_INIT:
        connect(SOCKET_ID, onenet_server_ip, onenet_port);
        
        break;
    case SOCK_ESTABLISHED:
        if(getSn_IR(SOCKET_ID) & Sn_IR_CON)
        {
            setSn_IR(SOCKET_ID, Sn_IR_CON);
        }
        if((len= getSn_RX_RSR(SOCKET_ID)) > 0)
        {
            if(len > DATA_BUF_SIZE)len = DATA_BUF_SIZE;
            len = recv(SOCKET_ID, ethernet_buf, len);
            ethernet_buf[len]=0x00;
            printf("recv:%s\n",ethernet_buf);
        }
        len = HTTP_PostPkt(ethernet_buf, apikey, onenet_id, "test", &test);
        printf("%s\n",ethernet_buf);
        send(SOCKET_ID, ethernet_buf, len);
        sleep_ms(500);

        break;
    case SOCK_CLOSE_WAIT:
        close(SOCKET_ID);
        
        break;
    case SOCK_CLOSED:
        socket(SOCKET_ID, Sn_MR_TCP, anyport++, 0x00);
        
        break;
    default:
        break;
    }
}


/**
*@brief		OneNet 数据打包函数
*@param		无
*@return	无
*/

uint32_t HTTP_PostPkt(char *pkt, char *key, char *devid, char *dsid, char *val)
{
	char dataBuf[100] = {0};
	char lenBuf[10] = {0};
	*pkt = 0;

	sprintf( dataBuf, "{\"%s\":%d}", dsid, *val);    										/*采用分割字符串格式:type = 3*/
	sprintf(lenBuf, "%d", strlen(dataBuf));

	strcat(pkt, "POST /devices/");
	strcat(pkt, devid);
	strcat(pkt, "/datapoints?type=3 HTTP/1.1\r\n");

	strcat(pkt, "api-key:");
	strcat(pkt, key);
	strcat(pkt, "\r\n");  

	strcat(pkt, "Host:api.heclouds.com\r\n");

	strcat(pkt, "Content-Length:");
	strcat(pkt, lenBuf);
	strcat(pkt, "\r\n\r\n");

	strcat(pkt, dataBuf);

	return strlen(pkt);
}
